package net.wanglu.www.zzz.rule.poss;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.tuple.Pair;

import net.wanglu.www.zzz.rule.BadEgg;
import net.wanglu.www.zzz.rule.Rule;
import net.wanglu.www.zzz.utils.WangluNLPUtils;
import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.tregex.TregexMatcher;
import edu.stanford.nlp.trees.tregex.TregexPattern;

public class WithoutApostrophe extends _PossessiveRule {

	@Override
	public Object applicable(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( super.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		Tree root = (Tree) preprocessData.get("root");
		
		sb.clear();
		sb.append("/\\bNP\\b/");
		sb.append("<");
		sb.append("/\\bNN\\b|\\bNNS\\b|\\bNNP\\b|\\bNNPS\\b/");
		TregexPattern pattern = TregexPattern.compile(sb.toString());
		TregexMatcher matcher = pattern.matcher(root);
		if ( !matcher.find()) {
			return Rule.Inapplicable;
		}
		return Rule.Applicable;
	}

	@Override
	public Object deviseRightSolution(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object examine(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		if ( this.applicable(objects) == Rule.Inapplicable) return Rule.Inapplicable;
		
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		
		Tree root = (Tree) preprocessData.get("root");
		
		sb.clear();
		sb.append("/\\bNP\\b/");
		sb.append("<");
		sb.append("/\\bNN\\b|\\bNNS\\b|\\bNNP\\b|\\bNNPS\\b/");
		TregexPattern pattern = TregexPattern.compile(sb.toString());
		TregexMatcher matcher = pattern.matcher(root);

		ArrayList<BadEgg> eggList = new ArrayList<BadEgg>(16);
		Tree NP = null;
		while( matcher.find()) {
			/*
			 * ---Start---(NP (DT the) (NNS traps) (NNS jaws))
			 * 有的NP节点下面有多个符合条件的NNS或NNP或NN或NNS，这样同一个NP就会出现多次，因此要多余的NP过滤掉。
			 */
			Tree curNP = matcher.getMatch();
			if ( curNP == NP) continue;
			NP = curNP;
			/*
			 * ---End---(NP (DT the) (NNS traps) (NNS jaws))
			 */			
			ArrayList<TaggedWord> twl = NP.taggedYield();
			for( int i=0; i<twl.size()-1; i++) {
				TaggedWord tw = twl.get(i);
				String word = tw.word();
				String tag = tw.tag();
				if ( !word.endsWith("s") || (!"JJ".equals(tag) && !"NNS".equals(tag) && !"NNP".equals(tag) && !"NNPS".equals(tag))) {
					continue;
				}
				
				if ( "JJ".equals(tag)) tag = "NNS";
				String lemma = WangluNLPUtils.lemmatization(word, tag);
				if ( lemma == null) lemma = WangluNLPUtils.stem(word);
				HashMap<?,?> map = (HashMap<?, ?>) WangluNLPUtils.sentencePreprocess(lemma);
				List<?> list = (List<?>) map.get("tags");
				String lemmaTag = (String) list.get(0);
				if ( "NNS".equals(lemmaTag) || "NN".equals(lemmaTag) || "NNPS".equals(lemmaTag) || "NNP".equals(lemmaTag)) {
					BadEgg egg = new BadEgg();
					egg.setViolatedRule(this);
					egg.whereIsWrong = Pair.of(tw, NP);
					eggList.add(egg);
				}
			}
		}

		if ( eggList.size() > 0) return eggList;
		return null;
	}

	@Override
	public Object whyItsWrong(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		BadEgg egg = (BadEgg) objects[0];
		
		Pair<?,?> pair = (Pair<?,?>) egg.whereIsWrong;
		TaggedWord tw = (TaggedWord) pair.getKey();
		sb.clear();
		sb.append("<p>");
		sb.append(tw.word());
		sb.append("是不是该添加一个单撇号，变成名词所有格的形式。");
		sb.append("</p>");
		
		return sb.toString();
	}

}
